﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreEx.Database.Client;
using LinFu.IoC.Configuration;
using LinFu.IoC.Interfaces;
using LinFu.IoC;

namespace CoreEx.Database.Linq.Implementation
{
    
    [Factory(typeof(ITable<>))]
    public class TableFactory : IFactory
    {
        public object CreateInstance(IFactoryRequest request)
        {
            var connectionInfo = request.Container.GetService<IConnectionInfo>(request.ServiceName);
            var tableType = typeof(Table<>).MakeGenericType(request.ServiceType.GetGenericArguments());
            if (request.Arguments.Length == 2)            
                return Activator.CreateInstance(tableType, new[] { connectionInfo.Name, request.Arguments[0], request.Arguments[1] });

            return Activator.CreateInstance(tableType, new[] { connectionInfo.Name});                                     
        }
    }
}
